{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第1章 Pandas基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 查看Pandas版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'1.1.1'"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "pd.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、文件读取与写入\n",
    "### 1. 读取\n",
    "#### （a）csv格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "  School Class    ID Gender   Address  Height  Weight  Math Physics\n0    S_1   C_1  1101      M  street_1     173      63  34.0      A+\n1    S_1   C_1  1102      F  street_2     192      73  32.5      B+\n2    S_1   C_1  1103      M  street_2     186      82  87.2      B+\n3    S_1   C_1  1104      F  street_2     167      81  80.4      B-\n4    S_1   C_1  1105      F  street_4     159      64  84.8      B+",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1101</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1102</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1103</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1104</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1105</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "df = pd.read_csv('data/table.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （b）txt格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   col1 col2  col3    col4\n0     2    a   1.4   apple\n1     3    b   3.4  banana\n2     6    c   2.5  orange\n3     5    d   3.2   lemon",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>col1</th>\n      <th>col2</th>\n      <th>col3</th>\n      <th>col4</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2</td>\n      <td>a</td>\n      <td>1.4</td>\n      <td>apple</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>3</td>\n      <td>b</td>\n      <td>3.4</td>\n      <td>banana</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>6</td>\n      <td>c</td>\n      <td>2.5</td>\n      <td>orange</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>5</td>\n      <td>d</td>\n      <td>3.2</td>\n      <td>lemon</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "df_txt = pd.read_table('data/table.txt') #可设置sep分隔符参数\n",
    "df_txt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （c）xls或xlsx格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "  School Class    ID Gender   Address  Height  Weight  Math Physics\n0    S_1   C_1  1101      M  street_1     173      63  34.0      A+\n1    S_1   C_1  1102      F  street_2     192      73  32.5      B+\n2    S_1   C_1  1103      M  street_2     186      82  87.2      B+\n3    S_1   C_1  1104      F  street_2     167      81  80.4      B-\n4    S_1   C_1  1105      F  street_4     159      64  84.8      B+",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1101</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1102</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1103</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1104</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1105</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "#需要安装xlrd包\n",
    "df_excel = pd.read_excel('data/table.xlsx')\n",
    "df_excel.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 写入"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （a）csv格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('data/new_table.csv')\n",
    "#df.to_csv('data/new_table.csv', index=False) #保存时除去行索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （b）xls或xlsx格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#需要安装openpyxl\n",
    "df.to_excel('data/new_table2.xlsx', sheet_name='Sheet1')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、基本数据结构\n",
    "### 1. Series\n",
    "#### （a）创建一个Series"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 对于一个Series，其中最常用的属性为值（values），索引（index），名字（name），类型（dtype）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "a    1.640777\nb    1.125966\nc   -1.203497\nd   -0.366809\ne    2.540860\nName: 这是一个Series, dtype: float64"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "s = pd.Series(np.random.randn(5),index=['a','b','c','d','e'],name='这是一个Series',dtype='float64')\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （b）访问Series属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 1.64077652,  1.12596647, -1.20349706, -0.36680934,  2.54085976])"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "s.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'这是一个Series'"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "s.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "s.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "dtype('float64')"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "s.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （c）取出某一个元素\n",
    "#### 将在第2章详细讨论索引的应用，这里先大致了解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "1.640776524075292"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "s['a']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （d）调用方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.7474592708586136"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "s.mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Series有相当多的方法可以调用："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "['T', 'a', 'abs', 'add', 'add_prefix', 'add_suffix', 'agg', 'aggregate', 'align', 'all', 'any', 'append', 'apply', 'argmax', 'argmin', 'argsort', 'array', 'asfreq', 'asof', 'astype', 'at', 'at_time', 'attrs', 'autocorr', 'axes', 'b', 'backfill', 'between', 'between_time', 'bfill', 'bool', 'c', 'clip', 'combine', 'combine_first', 'compare', 'convert_dtypes', 'copy', 'corr', 'count', 'cov', 'cummax', 'cummin', 'cumprod', 'cumsum', 'd', 'describe', 'diff', 'div', 'divide', 'divmod', 'dot', 'drop', 'drop_duplicates', 'droplevel', 'dropna', 'dtype', 'dtypes', 'duplicated', 'e', 'empty', 'eq', 'equals', 'ewm', 'expanding', 'explode', 'factorize', 'ffill', 'fillna', 'filter', 'first', 'first_valid_index', 'floordiv', 'ge', 'get', 'groupby', 'gt', 'hasnans', 'head', 'hist', 'iat', 'idxmax', 'idxmin', 'iloc', 'index', 'infer_objects', 'interpolate', 'is_monotonic', 'is_monotonic_decreasing', 'is_monotonic_increasing', 'is_unique', 'isin', 'isna', 'isnull', 'item', 'items', 'iteritems', 'keys', 'kurt', 'kurtosis', 'last', 'last_valid_index', 'le', 'loc', 'lt', 'mad', 'map', 'mask', 'max', 'mean', 'median', 'memory_usage', 'min', 'mod', 'mode', 'mul', 'multiply', 'name', 'nbytes', 'ndim', 'ne', 'nlargest', 'notna', 'notnull', 'nsmallest', 'nunique', 'pad', 'pct_change', 'pipe', 'plot', 'pop', 'pow', 'prod', 'product', 'quantile', 'radd', 'rank', 'ravel', 'rdiv', 'rdivmod', 'reindex', 'reindex_like', 'rename', 'rename_axis', 'reorder_levels', 'repeat', 'replace', 'resample', 'reset_index', 'rfloordiv', 'rmod', 'rmul', 'rolling', 'round', 'rpow', 'rsub', 'rtruediv', 'sample', 'searchsorted', 'sem', 'set_axis', 'shape', 'shift', 'size', 'skew', 'slice_shift', 'sort_index', 'sort_values', 'squeeze', 'std', 'sub', 'subtract', 'sum', 'swapaxes', 'swaplevel', 'tail', 'take', 'to_clipboard', 'to_csv', 'to_dict', 'to_excel', 'to_frame', 'to_hdf', 'to_json', 'to_latex', 'to_list', 'to_markdown', 'to_numpy', 'to_period', 'to_pickle', 'to_sql', 'to_string', 'to_timestamp', 'to_xarray', 'transform', 'transpose', 'truediv', 'truncate', 'tz_convert', 'tz_localize', 'unique', 'unstack', 'update', 'value_counts', 'values', 'var', 'view', 'where', 'xs']\n"
    }
   ],
   "source": [
    "print([attr for attr in dir(s) if not attr.startswith('_')])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. DataFrame\n",
    "#### （a）创建一个DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "  col1  col2  col3\n一    a     5   1.3\n二    b     6   2.5\n三    c     7   3.6\n四    d     8   4.6\n五    e     9   5.8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>col1</th>\n      <th>col2</th>\n      <th>col3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>一</th>\n      <td>a</td>\n      <td>5</td>\n      <td>1.3</td>\n    </tr>\n    <tr>\n      <th>二</th>\n      <td>b</td>\n      <td>6</td>\n      <td>2.5</td>\n    </tr>\n    <tr>\n      <th>三</th>\n      <td>c</td>\n      <td>7</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>四</th>\n      <td>d</td>\n      <td>8</td>\n      <td>4.6</td>\n    </tr>\n    <tr>\n      <th>五</th>\n      <td>e</td>\n      <td>9</td>\n      <td>5.8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "source": [
    "df = pd.DataFrame({'col1':list('abcde'),'col2':range(5,10),'col3':[1.3,2.5,3.6,4.6,5.8]},\n",
    "                 index=list('一二三四五'))\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （b）从DataFrame取出一列为Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "一    a\n二    b\n三    c\n四    d\n五    e\nName: col1, dtype: object"
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "df['col1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "pandas.core.frame.DataFrame"
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "type(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "pandas.core.series.Series"
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "source": [
    "type(df['col1'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （c）修改行或列名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "    new_col1  col2  col3\none        a     5   1.3\n二          b     6   2.5\n三          c     7   3.6\n四          d     8   4.6\n五          e     9   5.8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>new_col1</th>\n      <th>col2</th>\n      <th>col3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>a</td>\n      <td>5</td>\n      <td>1.3</td>\n    </tr>\n    <tr>\n      <th>二</th>\n      <td>b</td>\n      <td>6</td>\n      <td>2.5</td>\n    </tr>\n    <tr>\n      <th>三</th>\n      <td>c</td>\n      <td>7</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>四</th>\n      <td>d</td>\n      <td>8</td>\n      <td>4.6</td>\n    </tr>\n    <tr>\n      <th>五</th>\n      <td>e</td>\n      <td>9</td>\n      <td>5.8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "df.rename(index={'一':'one'},columns={'col1':'new_col1'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （d）调用属性和方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "Index(['一', '二', '三', '四', '五'], dtype='object')"
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "source": [
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "Index(['col1', 'col2', 'col3'], dtype='object')"
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([['a', 5, 1.3],\n       ['b', 6, 2.5],\n       ['c', 7, 3.6],\n       ['d', 8, 4.6],\n       ['e', 9, 5.8]], dtype=object)"
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "df.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(5, 3)"
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "col2    7.00\ncol3    3.56\ndtype: float64"
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "source": [
    "df.mean() #本质上是一种Aggregation操作，将在第3章详细介绍"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （e）索引对齐特性\n",
    "#### 这是Pandas中非常强大的特性，不理解这一特性有时就会造成一些麻烦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   A\n1 -1\n2 -1\n3  2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>-1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>-1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "source": [
    "df1 = pd.DataFrame({'A':[1,2,3]},index=[1,2,3])\n",
    "df2 = pd.DataFrame({'A':[1,2,3]},index=[3,1,2])\n",
    "df1-df2 #由于索引对齐，因此结果不是0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （f）列的删除与添加\n",
    "#### 对于删除而言，可以使用drop函数或del或pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   col2  col3\n一     5   1.3\n二     6   2.5\n三     7   3.6\n四     8   4.6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>col2</th>\n      <th>col3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>一</th>\n      <td>5</td>\n      <td>1.3</td>\n    </tr>\n    <tr>\n      <th>二</th>\n      <td>6</td>\n      <td>2.5</td>\n    </tr>\n    <tr>\n      <th>三</th>\n      <td>7</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>四</th>\n      <td>8</td>\n      <td>4.6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "source": [
    "df.drop(index='五',columns='col1') #设置inplace=True后会直接在原DataFrame中改动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   col2  col3\n一     5   1.3\n二     6   2.5\n三     7   3.6\n四     8   4.6\n五     9   5.8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>col2</th>\n      <th>col3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>一</th>\n      <td>5</td>\n      <td>1.3</td>\n    </tr>\n    <tr>\n      <th>二</th>\n      <td>6</td>\n      <td>2.5</td>\n    </tr>\n    <tr>\n      <th>三</th>\n      <td>7</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>四</th>\n      <td>8</td>\n      <td>4.6</td>\n    </tr>\n    <tr>\n      <th>五</th>\n      <td>9</td>\n      <td>5.8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "source": [
    "df['col1']=[1,2,3,4,5]\n",
    "del df['col1']\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### pop方法直接在原来的DataFrame上操作，且返回被删除的列，与python中的pop函数类似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "一    1\n二    2\n三    3\n四    4\n五    5\nName: col1, dtype: int64"
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "source": [
    "df['col1']=[1,2,3,4,5]\n",
    "df.pop('col1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   col2  col3\n一     5   1.3\n二     6   2.5\n三     7   3.6\n四     8   4.6\n五     9   5.8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>col2</th>\n      <th>col3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>一</th>\n      <td>5</td>\n      <td>1.3</td>\n    </tr>\n    <tr>\n      <th>二</th>\n      <td>6</td>\n      <td>2.5</td>\n    </tr>\n    <tr>\n      <th>三</th>\n      <td>7</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>四</th>\n      <td>8</td>\n      <td>4.6</td>\n    </tr>\n    <tr>\n      <th>五</th>\n      <td>9</td>\n      <td>5.8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 30
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 可以直接增加新的列，也可以使用assign方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   A  B\n1  1  a\n2  2  b\n3  3  c",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>a</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>b</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>c</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "source": [
    "df1['B']=list('abc')\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   A  B    C\n1  1  a    e\n2  2  b    f\n3  3  c  NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>a</td>\n      <td>e</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>b</td>\n      <td>f</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>c</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 32
    }
   ],
   "source": [
    "df1.assign(C=pd.Series(list('def')))\n",
    "#思考：为什么会出现NaN？（提示：索引对齐）assign左右两边的索引不一样，请问结果的索引谁说了算？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 但assign方法不会对原DataFrame做修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   A  B\n1  1  a\n2  2  b\n3  3  c",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>a</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>b</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>c</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 33
    }
   ],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （g）根据类型选择列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   col2  col3\n一     5   1.3\n二     6   2.5\n三     7   3.6\n四     8   4.6\n五     9   5.8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>col2</th>\n      <th>col3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>一</th>\n      <td>5</td>\n      <td>1.3</td>\n    </tr>\n    <tr>\n      <th>二</th>\n      <td>6</td>\n      <td>2.5</td>\n    </tr>\n    <tr>\n      <th>三</th>\n      <td>7</td>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>四</th>\n      <td>8</td>\n      <td>4.6</td>\n    </tr>\n    <tr>\n      <th>五</th>\n      <td>9</td>\n      <td>5.8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 34
    }
   ],
   "source": [
    "df.select_dtypes(include=['number']).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   col3\n一   1.3\n二   2.5\n三   3.6\n四   4.6\n五   5.8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>col3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>一</th>\n      <td>1.3</td>\n    </tr>\n    <tr>\n      <th>二</th>\n      <td>2.5</td>\n    </tr>\n    <tr>\n      <th>三</th>\n      <td>3.6</td>\n    </tr>\n    <tr>\n      <th>四</th>\n      <td>4.6</td>\n    </tr>\n    <tr>\n      <th>五</th>\n      <td>5.8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 35
    }
   ],
   "source": [
    "df.select_dtypes(include=['float']).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （h）将Series转换为DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "col2    7.00\ncol3    3.56\nName: to_DataFrame, dtype: float64"
     },
     "metadata": {},
     "execution_count": 36
    }
   ],
   "source": [
    "s = df.mean()\n",
    "s.name='to_DataFrame'\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "      to_DataFrame\ncol2          7.00\ncol3          3.56",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>to_DataFrame</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>col2</th>\n      <td>7.00</td>\n    </tr>\n    <tr>\n      <th>col3</th>\n      <td>3.56</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 37
    }
   ],
   "source": [
    "s.to_frame()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 使用T符号可以转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "              col2  col3\nto_DataFrame   7.0  3.56",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>col2</th>\n      <th>col3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>to_DataFrame</th>\n      <td>7.0</td>\n      <td>3.56</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 38
    }
   ],
   "source": [
    "s.to_frame().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 三、常用基本函数\n",
    "#### 从下面开始，包括后面所有章节，我们都会用到这份虚拟的数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('data/table.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. head和tail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "  School Class    ID Gender   Address  Height  Weight  Math Physics\n0    S_1   C_1  1101      M  street_1     173      63  34.0      A+\n1    S_1   C_1  1102      F  street_2     192      73  32.5      B+\n2    S_1   C_1  1103      M  street_2     186      82  87.2      B+\n3    S_1   C_1  1104      F  street_2     167      81  80.4      B-\n4    S_1   C_1  1105      F  street_4     159      64  84.8      B+",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1101</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1102</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1103</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1104</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1105</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 40
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   School Class    ID Gender   Address  Height  Weight  Math Physics\n30    S_2   C_4  2401      F  street_2     192      62  45.3       A\n31    S_2   C_4  2402      M  street_7     166      82  48.7       B\n32    S_2   C_4  2403      F  street_6     158      60  59.7      B+\n33    S_2   C_4  2404      F  street_2     160      84  67.7       B\n34    S_2   C_4  2405      F  street_6     193      54  47.6       B",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>30</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>2401</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>62</td>\n      <td>45.3</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>31</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>2402</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>166</td>\n      <td>82</td>\n      <td>48.7</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>32</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>2403</td>\n      <td>F</td>\n      <td>street_6</td>\n      <td>158</td>\n      <td>60</td>\n      <td>59.7</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>33</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>2404</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>160</td>\n      <td>84</td>\n      <td>67.7</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>34</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>2405</td>\n      <td>F</td>\n      <td>street_6</td>\n      <td>193</td>\n      <td>54</td>\n      <td>47.6</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 41
    }
   ],
   "source": [
    "df.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 可以指定n参数显示多少行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "  School Class    ID Gender   Address  Height  Weight  Math Physics\n0    S_1   C_1  1101      M  street_1     173      63  34.0      A+\n1    S_1   C_1  1102      F  street_2     192      73  32.5      B+\n2    S_1   C_1  1103      M  street_2     186      82  87.2      B+",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1101</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1102</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1103</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 42
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. unique和nunique"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### nunique显示有多少个唯一值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "7"
     },
     "metadata": {},
     "execution_count": 43
    }
   ],
   "source": [
    "df['Physics'].nunique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### unique显示所有的唯一值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array(['A+', 'B+', 'B-', 'A-', 'B', 'A', 'C'], dtype=object)"
     },
     "metadata": {},
     "execution_count": 44
    }
   ],
   "source": [
    "df['Physics'].unique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. count和value_counts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### count返回非缺失值元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "35"
     },
     "metadata": {},
     "execution_count": 45
    }
   ],
   "source": [
    "df['Physics'].count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### value_counts返回每个元素有多少个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "B+    9\nB     8\nB-    6\nA     4\nA+    3\nA-    3\nC     2\nName: Physics, dtype: int64"
     },
     "metadata": {},
     "execution_count": 46
    }
   ],
   "source": [
    "df['Physics'].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. describe和info"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### info函数返回有哪些列、有多少非缺失值、每列的类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "<class 'pandas.core.frame.DataFrame'>\nRangeIndex: 35 entries, 0 to 34\nData columns (total 9 columns):\n #   Column   Non-Null Count  Dtype  \n---  ------   --------------  -----  \n 0   School   35 non-null     object \n 1   Class    35 non-null     object \n 2   ID       35 non-null     int64  \n 3   Gender   35 non-null     object \n 4   Address  35 non-null     object \n 5   Height   35 non-null     int64  \n 6   Weight   35 non-null     int64  \n 7   Math     35 non-null     float64\n 8   Physics  35 non-null     object \ndtypes: float64(1), int64(3), object(5)\nmemory usage: 2.6+ KB\n"
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### describe默认统计数值型数据的各个统计量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "               ID      Height      Weight       Math\ncount    35.00000   35.000000   35.000000  35.000000\nmean   1803.00000  174.142857   74.657143  61.351429\nstd     536.87741   13.541098   12.895377  19.915164\nmin    1101.00000  155.000000   53.000000  31.500000\n25%    1204.50000  161.000000   63.000000  47.400000\n50%    2103.00000  173.000000   74.000000  61.700000\n75%    2301.50000  187.500000   82.000000  77.100000\nmax    2405.00000  195.000000  100.000000  97.000000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>ID</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>35.00000</td>\n      <td>35.000000</td>\n      <td>35.000000</td>\n      <td>35.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>1803.00000</td>\n      <td>174.142857</td>\n      <td>74.657143</td>\n      <td>61.351429</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>536.87741</td>\n      <td>13.541098</td>\n      <td>12.895377</td>\n      <td>19.915164</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>1101.00000</td>\n      <td>155.000000</td>\n      <td>53.000000</td>\n      <td>31.500000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>1204.50000</td>\n      <td>161.000000</td>\n      <td>63.000000</td>\n      <td>47.400000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>2103.00000</td>\n      <td>173.000000</td>\n      <td>74.000000</td>\n      <td>61.700000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>2301.50000</td>\n      <td>187.500000</td>\n      <td>82.000000</td>\n      <td>77.100000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>2405.00000</td>\n      <td>195.000000</td>\n      <td>100.000000</td>\n      <td>97.000000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 48
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 可以自行选择分位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "               ID      Height      Weight       Math\ncount    35.00000   35.000000   35.000000  35.000000\nmean   1803.00000  174.142857   74.657143  61.351429\nstd     536.87741   13.541098   12.895377  19.915164\nmin    1101.00000  155.000000   53.000000  31.500000\n5%     1102.70000  157.000000   56.100000  32.640000\n25%    1204.50000  161.000000   63.000000  47.400000\n50%    2103.00000  173.000000   74.000000  61.700000\n75%    2301.50000  187.500000   82.000000  77.100000\n95%    2403.30000  193.300000   97.600000  90.040000\nmax    2405.00000  195.000000  100.000000  97.000000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>ID</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>35.00000</td>\n      <td>35.000000</td>\n      <td>35.000000</td>\n      <td>35.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>1803.00000</td>\n      <td>174.142857</td>\n      <td>74.657143</td>\n      <td>61.351429</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>536.87741</td>\n      <td>13.541098</td>\n      <td>12.895377</td>\n      <td>19.915164</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>1101.00000</td>\n      <td>155.000000</td>\n      <td>53.000000</td>\n      <td>31.500000</td>\n    </tr>\n    <tr>\n      <th>5%</th>\n      <td>1102.70000</td>\n      <td>157.000000</td>\n      <td>56.100000</td>\n      <td>32.640000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>1204.50000</td>\n      <td>161.000000</td>\n      <td>63.000000</td>\n      <td>47.400000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>2103.00000</td>\n      <td>173.000000</td>\n      <td>74.000000</td>\n      <td>61.700000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>2301.50000</td>\n      <td>187.500000</td>\n      <td>82.000000</td>\n      <td>77.100000</td>\n    </tr>\n    <tr>\n      <th>95%</th>\n      <td>2403.30000</td>\n      <td>193.300000</td>\n      <td>97.600000</td>\n      <td>90.040000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>2405.00000</td>\n      <td>195.000000</td>\n      <td>100.000000</td>\n      <td>97.000000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 49
    }
   ],
   "source": [
    "df.describe(percentiles=[.05, .25, .75, .95])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 对于非数值型也可以用describe函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "count     35\nunique     7\ntop       B+\nfreq       9\nName: Physics, dtype: object"
     },
     "metadata": {},
     "execution_count": 50
    }
   ],
   "source": [
    "df['Physics'].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. idxmax和nlargest\n",
    "#### idxmax函数返回最大值所在索引，在某些情况下特别适用，idxmin功能类似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "5"
     },
     "metadata": {},
     "execution_count": 51
    }
   ],
   "source": [
    "df['Math'].idxmax()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### nlargest函数返回前几个大的元素值，nsmallest功能类似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "5     97.0\n28    95.5\n11    87.7\nName: Math, dtype: float64"
     },
     "metadata": {},
     "execution_count": 52
    }
   ],
   "source": [
    "df['Math'].nlargest(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. clip和replace"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### clip和replace是两类替换函数\n",
    "#### clip是对超过或者低于某些值的数进行截断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0    34.0\n1    32.5\n2    87.2\n3    80.4\n4    84.8\nName: Math, dtype: float64"
     },
     "metadata": {},
     "execution_count": 53
    }
   ],
   "source": [
    "df['Math'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0    34.0\n1    33.0\n2    80.0\n3    80.0\n4    80.0\nName: Math, dtype: float64"
     },
     "metadata": {},
     "execution_count": 54
    }
   ],
   "source": [
    "df['Math'].clip(33,80).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "16.924244897959188"
     },
     "metadata": {},
     "execution_count": 55
    }
   ],
   "source": [
    "df['Math'].mad()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### replace是对某些值进行替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0    street_1\n1    street_2\n2    street_2\n3    street_2\n4    street_4\nName: Address, dtype: object"
     },
     "metadata": {},
     "execution_count": 56
    }
   ],
   "source": [
    "df['Address'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0         one\n1         two\n2         two\n3         two\n4    street_4\nName: Address, dtype: object"
     },
     "metadata": {},
     "execution_count": 57
    }
   ],
   "source": [
    "df['Address'].replace(['street_1','street_2'],['one','two']).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 通过字典，可以直接在表中修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "  School Class    ID Gender   Address  Height  Weight  Math Physics\n0    S_1   C_1  1101      M       one     173      63  34.0      A+\n1    S_1   C_1  1102      F       two     192      73  32.5      B+\n2    S_1   C_1  1103      M       two     186      82  87.2      B+\n3    S_1   C_1  1104      F       two     167      81  80.4      B-\n4    S_1   C_1  1105      F  street_4     159      64  84.8      B+",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1101</td>\n      <td>M</td>\n      <td>one</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1102</td>\n      <td>F</td>\n      <td>two</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1103</td>\n      <td>M</td>\n      <td>two</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1104</td>\n      <td>F</td>\n      <td>two</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1105</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 58
    }
   ],
   "source": [
    "df.replace({'Address':{'street_1':'one','street_2':'two'}}).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7. apply函数\n",
    "#### apply是一个自由度很高的函数，在第3章我们还要提到\n",
    "#### 对于Series，它可以迭代每一列的值操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0    34.0!\n1    32.5!\n2    87.2!\n3    80.4!\n4    84.8!\nName: Math, dtype: object"
     },
     "metadata": {},
     "execution_count": 59
    }
   ],
   "source": [
    "df['Math'].apply(lambda x:str(x)+'!').head() #可以使用lambda表达式，也可以使用函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 对于DataFrame，它在默认axis=0下可以迭代每一个列操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "  School Class     ID Gender    Address Height Weight   Math Physics\n0   S_1!  C_1!  1101!     M!  street_1!   173!    63!  34.0!     A+!\n1   S_1!  C_1!  1102!     F!  street_2!   192!    73!  32.5!     B+!\n2   S_1!  C_1!  1103!     M!  street_2!   186!    82!  87.2!     B+!\n3   S_1!  C_1!  1104!     F!  street_2!   167!    81!  80.4!     B-!\n4   S_1!  C_1!  1105!     F!  street_4!   159!    64!  84.8!     B+!",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>S_1!</td>\n      <td>C_1!</td>\n      <td>1101!</td>\n      <td>M!</td>\n      <td>street_1!</td>\n      <td>173!</td>\n      <td>63!</td>\n      <td>34.0!</td>\n      <td>A+!</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>S_1!</td>\n      <td>C_1!</td>\n      <td>1102!</td>\n      <td>F!</td>\n      <td>street_2!</td>\n      <td>192!</td>\n      <td>73!</td>\n      <td>32.5!</td>\n      <td>B+!</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>S_1!</td>\n      <td>C_1!</td>\n      <td>1103!</td>\n      <td>M!</td>\n      <td>street_2!</td>\n      <td>186!</td>\n      <td>82!</td>\n      <td>87.2!</td>\n      <td>B+!</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>S_1!</td>\n      <td>C_1!</td>\n      <td>1104!</td>\n      <td>F!</td>\n      <td>street_2!</td>\n      <td>167!</td>\n      <td>81!</td>\n      <td>80.4!</td>\n      <td>B-!</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>S_1!</td>\n      <td>C_1!</td>\n      <td>1105!</td>\n      <td>F!</td>\n      <td>street_4!</td>\n      <td>159!</td>\n      <td>64!</td>\n      <td>84.8!</td>\n      <td>B+!</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 60
    }
   ],
   "source": [
    "df.apply(lambda x:x.apply(lambda x:str(x)+'!')).head() #这是一个稍显复杂的例子，有利于理解apply的功能"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Pandas中的axis参数=0时，永远表示的是处理方向而不是聚合方向，当axis='index'或=0时，对列迭代对行聚合，行即为跨列，axis=1同理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 四、排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 索引排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "     School Class    ID Gender   Address  Height  Weight Physics\nMath                                                            \n34.0    S_1   C_1  1101      M  street_1     173      63      A+\n32.5    S_1   C_1  1102      F  street_2     192      73      B+\n87.2    S_1   C_1  1103      M  street_2     186      82      B+\n80.4    S_1   C_1  1104      F  street_2     167      81      B-\n84.8    S_1   C_1  1105      F  street_4     159      64      B+",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Math</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>34.0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1101</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>32.5</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1102</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>87.2</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1103</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>80.4</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1104</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>84.8</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1105</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 61
    }
   ],
   "source": [
    "df.set_index('Math').head() #set_index函数可以设置索引，将在下一章详细介绍"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "     School Class    ID Gender   Address  Height  Weight Physics\nMath                                                            \n31.5    S_1   C_3  1301      M  street_4     161      68      B+\n32.5    S_1   C_1  1102      F  street_2     192      73      B+\n32.7    S_2   C_3  2302      M  street_5     171      88       A\n33.8    S_1   C_2  1204      F  street_5     162      63       B\n34.0    S_1   C_1  1101      M  street_1     173      63      A+",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Math</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>31.5</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>1301</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>161</td>\n      <td>68</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>32.5</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1102</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>32.7</th>\n      <td>S_2</td>\n      <td>C_3</td>\n      <td>2302</td>\n      <td>M</td>\n      <td>street_5</td>\n      <td>171</td>\n      <td>88</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>33.8</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>1204</td>\n      <td>F</td>\n      <td>street_5</td>\n      <td>162</td>\n      <td>63</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>34.0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1101</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>A+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 62
    }
   ],
   "source": [
    "df.set_index('Math').sort_index().head() #可以设置ascending参数，默认为升序，True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 值排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   School Class    ID Gender   Address  Height  Weight  Math Physics\n0     S_1   C_1  1101      M  street_1     173      63  34.0      A+\n19    S_2   C_1  2105      M  street_4     170      81  34.2       A\n18    S_2   C_1  2104      F  street_5     159      97  72.2      B+\n16    S_2   C_1  2102      F  street_6     161      61  50.6      B+\n15    S_2   C_1  2101      M  street_7     174      84  83.3       C",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1101</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>19</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>2105</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>170</td>\n      <td>81</td>\n      <td>34.2</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>18</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>2104</td>\n      <td>F</td>\n      <td>street_5</td>\n      <td>159</td>\n      <td>97</td>\n      <td>72.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>16</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>2102</td>\n      <td>F</td>\n      <td>street_6</td>\n      <td>161</td>\n      <td>61</td>\n      <td>50.6</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>2101</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>174</td>\n      <td>84</td>\n      <td>83.3</td>\n      <td>C</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 63
    }
   ],
   "source": [
    "df.sort_values(by='Class').head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 多个值排序，即先对第一层排，在第一层相同的情况下对第二层排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "   School Class    ID Gender   Address  Height  Weight  Math Physics\n0     S_1   C_1  1101      M  street_1     173      63  34.0      A+\n11    S_1   C_3  1302      F  street_1     175      57  87.7      A-\n23    S_2   C_2  2204      M  street_1     175      74  47.2      B-\n33    S_2   C_4  2404      F  street_2     160      84  67.7       B\n3     S_1   C_1  1104      F  street_2     167      81  80.4      B-",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1101</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>1302</td>\n      <td>F</td>\n      <td>street_1</td>\n      <td>175</td>\n      <td>57</td>\n      <td>87.7</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>23</th>\n      <td>S_2</td>\n      <td>C_2</td>\n      <td>2204</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>175</td>\n      <td>74</td>\n      <td>47.2</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>33</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>2404</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>160</td>\n      <td>84</td>\n      <td>67.7</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>1104</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 64
    }
   ],
   "source": [
    "df.sort_values(by=['Address','Height']).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 五、问题与练习\n",
    "### 1. 问题\n",
    "#### 【问题一】 Series和DataFrame有哪些常见属性和方法？\n",
    "#### 【问题二】 value_counts会统计缺失值吗？\n",
    "#### 【问题三】 如果有多个索引同时取到最大值，idxmax会返回所有这些索引吗？如果不会，那么怎么返回这些索引？\n",
    "#### 【问题四】 在常用函数一节中，由于一些函数的功能比较简单，因此没有列入，现在将它们列在下面，请分别说明它们的用途并尝试使用。\n",
    "#### sum/mean/median/mad/min/max/abs/std/var/quantile/cummax/cumsum/cumprod\n",
    "#### 【问题五】 df.mean(axis=1)是什么意思？它与df.mean()的结果一样吗？问题四提到的函数也有axis参数吗？怎么使用？\n",
    "#### 【问题六】 对值进行排序后，相同的值次序由什么决定？\n",
    "#### 【问题七】 Pandas中为各类基础运算也定义了函数，比如s1.add(s2)表示两个Series相加，但既然已经有了'+'，是不是多此一举？\n",
    "#### 【问题八】 如果DataFrame某一列的元素是numpy数组，那么将其保存到csv在读取后就会变成字符串，怎么解决？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 练习\n",
    "#### 【练习一】 现有一份关于美剧《权力的游戏》剧本的数据集，请解决以下问题：\n",
    "#### （a）在所有的数据中，一共出现了多少人物？\n",
    "#### （b）以单元格计数（即简单把一个单元格视作一句），谁说了最多的话？\n",
    "#### （c）以单词计数，谁说了最多的单词？（不是单句单词最多，是指每人说过单词的总数最多，为了简便，只以空格为单词分界点，不考虑其他情况）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "  Release Date    Season    Episode     Episode Title          Name  \\\n0    2011/4/17  Season 1  Episode 1  Winter is Coming  waymar royce   \n1    2011/4/17  Season 1  Episode 1  Winter is Coming          will   \n2    2011/4/17  Season 1  Episode 1  Winter is Coming  waymar royce   \n3    2011/4/17  Season 1  Episode 1  Winter is Coming          will   \n4    2011/4/17  Season 1  Episode 1  Winter is Coming         gared   \n\n                                            Sentence  \n0  What do you expect? They're savages. One lot s...  \n1  I've never seen wildlings do a thing like this...  \n2                             How close did you get?  \n3                            Close as any man would.  \n4                   We should head back to the wall.  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Release Date</th>\n      <th>Season</th>\n      <th>Episode</th>\n      <th>Episode Title</th>\n      <th>Name</th>\n      <th>Sentence</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2011/4/17</td>\n      <td>Season 1</td>\n      <td>Episode 1</td>\n      <td>Winter is Coming</td>\n      <td>waymar royce</td>\n      <td>What do you expect? They're savages. One lot s...</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2011/4/17</td>\n      <td>Season 1</td>\n      <td>Episode 1</td>\n      <td>Winter is Coming</td>\n      <td>will</td>\n      <td>I've never seen wildlings do a thing like this...</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2011/4/17</td>\n      <td>Season 1</td>\n      <td>Episode 1</td>\n      <td>Winter is Coming</td>\n      <td>waymar royce</td>\n      <td>How close did you get?</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2011/4/17</td>\n      <td>Season 1</td>\n      <td>Episode 1</td>\n      <td>Winter is Coming</td>\n      <td>will</td>\n      <td>Close as any man would.</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2011/4/17</td>\n      <td>Season 1</td>\n      <td>Episode 1</td>\n      <td>Winter is Coming</td>\n      <td>gared</td>\n      <td>We should head back to the wall.</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 65
    }
   ],
   "source": [
    "pd.read_csv('data/Game_of_Thrones_Script.csv').head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 【练习二】现有一份关于科比的投篮数据集，请解决如下问题：\n",
    "#### （a）哪种action_type和combined_shot_type的组合是最多的？\n",
    "#### （b）在所有被记录的game_id中，遭遇到最多的opponent是一个支？（由于一场比赛会有许多次投篮，但对阵的对手只有一个，本题相当于问科比和哪个队交锋次数最多）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "               action_type combined_shot_type  game_event_id   game_id  \\\nshot_id                                                                  \n1                Jump Shot          Jump Shot             10  20000012   \n2                Jump Shot          Jump Shot             12  20000012   \n3                Jump Shot          Jump Shot             35  20000012   \n4                Jump Shot          Jump Shot             43  20000012   \n5        Driving Dunk Shot               Dunk            155  20000012   \n\n             lat  loc_x  loc_y       lon  minutes_remaining  period  ...  \\\nshot_id                                                              ...   \n1        33.9723    167     72 -118.1028                 10       1  ...   \n2        34.0443   -157      0 -118.4268                 10       1  ...   \n3        33.9093   -101    135 -118.3708                  7       1  ...   \n4        33.8693    138    175 -118.1318                  6       1  ...   \n5        34.0443      0      0 -118.2698                  6       2  ...   \n\n         shot_made_flag       shot_type         shot_zone_area  \\\nshot_id                                                          \n1                   NaN  2PT Field Goal          Right Side(R)   \n2                   0.0  2PT Field Goal           Left Side(L)   \n3                   1.0  2PT Field Goal   Left Side Center(LC)   \n4                   0.0  2PT Field Goal  Right Side Center(RC)   \n5                   1.0  2PT Field Goal              Center(C)   \n\n         shot_zone_basic  shot_zone_range     team_id           team_name  \\\nshot_id                                                                     \n1              Mid-Range        16-24 ft.  1610612747  Los Angeles Lakers   \n2              Mid-Range         8-16 ft.  1610612747  Los Angeles Lakers   \n3              Mid-Range        16-24 ft.  1610612747  Los Angeles Lakers   \n4              Mid-Range        16-24 ft.  1610612747  Los Angeles Lakers   \n5        Restricted Area  Less Than 8 ft.  1610612747  Los Angeles Lakers   \n\n          game_date    matchup  opponent  \nshot_id                                   \n1        2000/10/31  LAL @ POR       POR  \n2        2000/10/31  LAL @ POR       POR  \n3        2000/10/31  LAL @ POR       POR  \n4        2000/10/31  LAL @ POR       POR  \n5        2000/10/31  LAL @ POR       POR  \n\n[5 rows x 24 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>action_type</th>\n      <th>combined_shot_type</th>\n      <th>game_event_id</th>\n      <th>game_id</th>\n      <th>lat</th>\n      <th>loc_x</th>\n      <th>loc_y</th>\n      <th>lon</th>\n      <th>minutes_remaining</th>\n      <th>period</th>\n      <th>...</th>\n      <th>shot_made_flag</th>\n      <th>shot_type</th>\n      <th>shot_zone_area</th>\n      <th>shot_zone_basic</th>\n      <th>shot_zone_range</th>\n      <th>team_id</th>\n      <th>team_name</th>\n      <th>game_date</th>\n      <th>matchup</th>\n      <th>opponent</th>\n    </tr>\n    <tr>\n      <th>shot_id</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>Jump Shot</td>\n      <td>Jump Shot</td>\n      <td>10</td>\n      <td>20000012</td>\n      <td>33.9723</td>\n      <td>167</td>\n      <td>72</td>\n      <td>-118.1028</td>\n      <td>10</td>\n      <td>1</td>\n      <td>...</td>\n      <td>NaN</td>\n      <td>2PT Field Goal</td>\n      <td>Right Side(R)</td>\n      <td>Mid-Range</td>\n      <td>16-24 ft.</td>\n      <td>1610612747</td>\n      <td>Los Angeles Lakers</td>\n      <td>2000/10/31</td>\n      <td>LAL @ POR</td>\n      <td>POR</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Jump Shot</td>\n      <td>Jump Shot</td>\n      <td>12</td>\n      <td>20000012</td>\n      <td>34.0443</td>\n      <td>-157</td>\n      <td>0</td>\n      <td>-118.4268</td>\n      <td>10</td>\n      <td>1</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>2PT Field Goal</td>\n      <td>Left Side(L)</td>\n      <td>Mid-Range</td>\n      <td>8-16 ft.</td>\n      <td>1610612747</td>\n      <td>Los Angeles Lakers</td>\n      <td>2000/10/31</td>\n      <td>LAL @ POR</td>\n      <td>POR</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Jump Shot</td>\n      <td>Jump Shot</td>\n      <td>35</td>\n      <td>20000012</td>\n      <td>33.9093</td>\n      <td>-101</td>\n      <td>135</td>\n      <td>-118.3708</td>\n      <td>7</td>\n      <td>1</td>\n      <td>...</td>\n      <td>1.0</td>\n      <td>2PT Field Goal</td>\n      <td>Left Side Center(LC)</td>\n      <td>Mid-Range</td>\n      <td>16-24 ft.</td>\n      <td>1610612747</td>\n      <td>Los Angeles Lakers</td>\n      <td>2000/10/31</td>\n      <td>LAL @ POR</td>\n      <td>POR</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Jump Shot</td>\n      <td>Jump Shot</td>\n      <td>43</td>\n      <td>20000012</td>\n      <td>33.8693</td>\n      <td>138</td>\n      <td>175</td>\n      <td>-118.1318</td>\n      <td>6</td>\n      <td>1</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>2PT Field Goal</td>\n      <td>Right Side Center(RC)</td>\n      <td>Mid-Range</td>\n      <td>16-24 ft.</td>\n      <td>1610612747</td>\n      <td>Los Angeles Lakers</td>\n      <td>2000/10/31</td>\n      <td>LAL @ POR</td>\n      <td>POR</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>Driving Dunk Shot</td>\n      <td>Dunk</td>\n      <td>155</td>\n      <td>20000012</td>\n      <td>34.0443</td>\n      <td>0</td>\n      <td>0</td>\n      <td>-118.2698</td>\n      <td>6</td>\n      <td>2</td>\n      <td>...</td>\n      <td>1.0</td>\n      <td>2PT Field Goal</td>\n      <td>Center(C)</td>\n      <td>Restricted Area</td>\n      <td>Less Than 8 ft.</td>\n      <td>1610612747</td>\n      <td>Los Angeles Lakers</td>\n      <td>2000/10/31</td>\n      <td>LAL @ POR</td>\n      <td>POR</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 24 columns</p>\n</div>"
     },
     "metadata": {},
     "execution_count": 66
    }
   ],
   "source": [
    "pd.read_csv('data/Kobe_data.csv',index_col='shot_id').head()\n",
    "#index_col的作用是将某一列作为行索引"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PyCharm (pythonProject)",
   "language": "python",
   "name": "pycharm-f7427e7c"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}